package com.wh1200.mybatis.xmlless

import cn.hutool.core.annotation.AnnotationUtil
import cn.hutool.core.util.ReflectUtil
import com.wh1200.mybatis.xmlless.annotations.JsonMappingProperty
import com.fasterxml.jackson.annotation.JsonProperty
import jakarta.persistence.GeneratedValue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.core.annotation.AnnotationUtils
import kotlin.reflect.jvm.javaField
import kotlin.test.assertNotNull

/**
 *
 * @author 吴昊
 * @date 2023/12/8 13:51
 * @since v0.0.0
 * @version 1.0
 */
class AnnotationTest {

  @Test
  @DisplayName("测试获取注解")
  fun getAnno() {
    find(JsonMappingProperty::class.java)
    find(JsonProperty::class.java)
  }

  @Test
  fun test() {
    val field = Bean::id.javaField!!
    field.annotations.forEach {
      val method = ReflectUtil.getMethodOfObj(it, "generator", *arrayOfNulls<Any>(0))
      val value = ReflectUtil.invoke<String>(it, method, *arrayOfNulls<Any>(0))
      println(value)
    }
  }

  private fun find(clazz: Class<out Annotation>) {
    assertNotNull(AnnotationUtils.getAnnotation(Bean::a.javaField!!, clazz))
    assertNotNull(AnnotationUtils.getAnnotation(Bean::b.javaField!!, clazz))
    assertNotNull(AnnotationUtils.getAnnotation(Bean::c.javaField!!, clazz))
    assertNotNull(AnnotationUtils.getAnnotation(Bean::d.javaField!!, clazz))
  }

  class Bean(
      @JsonMappingProperty @field: JsonProperty var c: String = "",
      @JsonMappingProperty @field: JsonProperty val d: String = "",
      @GeneratedValue(
          generator = "uuid"
      )
      val id2: String = ""
  ) {

    @JsonMappingProperty
    @field: JsonProperty
    var a: String = ""

    @JsonMappingProperty
    @JsonProperty
    val b: String = ""

    @GeneratedValue(
        generator = "uuid"
    )
    val id: String = ""

  }

}
